package com.hk.aefz.service.impl;

import com.hk.aefz.config.Uploadconfig;
import com.hk.aefz.service.UploadFileService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import java.io.File;
import java.io.IOException;
import java.util.UUID;

@Service
@EnableConfigurationProperties(Uploadconfig.class)
public class UploadFileServiceImpl implements UploadFileService {

    @Autowired
    private Uploadconfig uploadconfig;

    @Override
    public String UploadFile(MultipartFile upload) throws IOException, InterruptedException {
//        图片存储绝对路径
        String path = "D:/Software/Software/IDEA/aefzblog/hk-upload/src/main/resources/static/";
        File file1 = new File(path);
        if (!file1.exists()) {
            file1.mkdirs(); //不存在创建
        }
        //        获取图片名称及类型
        String filename = upload.getOriginalFilename();
        String s = UUID.randomUUID().toString().replaceAll("-","").substring(0,5);
        filename = s+"_"+filename;
        File file = new File(path, filename);
        upload.transferTo(file);//上传图片
        Thread.sleep(1500); //睡眠1.5秒
        String surl = uploadconfig.getBaseurl()+filename;
        return surl;
    }
}
